package com.itboye.jinpuyiyuan.activity.home.finddoctor;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.itboye.jinpuyiyuan.R;
import com.itboye.jinpuyiyuan.adapter.FindDoctorArrayAdapter;
import com.itboye.jinpuyiyuan.adapter.RegisterDoctorDetailAdapter;
import com.itboye.jinpuyiyuan.base.BaseActivity;
import com.itboye.jinpuyiyuan.bean.QueryDoctorListBean;
import com.itboye.jinpuyiyuan.bean.RegisterDoctorDetail;
import com.itboye.jinpuyiyuan.presenter.BranchAddressPresenter;
import com.itboye.jinpuyiyuan.utils.volley.ResultEntity;
import com.itboye.jinpuyiyuan.widget.AutoResizeTextView;
import com.itboye.jinpuyiyuan.widget.loadingutil.MAlert;

import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;

/**
 * Created by Administrator on 2017/12/11 0011.
 */

public class FindDoctorActivity extends BaseActivity implements Observer {
    //title
    private ImageView leftImg,rightImg;
    private TextView leftTv,tvTitle;
    private RelativeLayout rlLeft,toobar;
    private AutoResizeTextView branchSelect,typeSelect,departmentSelect;
    private TextView branchSelectTv,typeSelectTv,departmentSelectTv;
    private ListView findDoctorList,branchList,departmentList,typeList;
    private LinearLayout branchLL,typeLL,departmentLL,branchSelectLL,typeSelectLL,departmentSelectLL;

    private ImageView movableLine;
    private int moveDistance;//移动的距离
    private int currIndex;

    private int num;//记录点击次数
    private int id=0;//记录点击的view id
    private ArrayList<String> branchArray,departmentArray,typeArray;
    private ArrayList<QueryDoctorListBean.RegisterDoctorChild> doctorList;
    private RegisterDoctorDetailAdapter registerDoctorDetailAdapter;
    private FindDoctorArrayAdapter branchAdapter,departmentAdapter,typeAdapter;
    private BranchAddressPresenter presenter;
    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);
        setContentView(R.layout.activity_finddoctor);
        initView();
        initViewData();
        currIndex=0;
        Log.d("==========",moveDistance+"");


    }

    private void initView() {
        rightImg.setVisibility(View.GONE);
        leftTv.setVisibility(View.GONE);
        tvTitle.setText("金普名医");
        branchLL.setVisibility(View.GONE);
        departmentLL.setVisibility(View.GONE);
        typeLL.setVisibility(View.GONE);

        branchArray=new ArrayList<>();
        departmentArray=new ArrayList<>();
        doctorList=new ArrayList<>();
        typeArray=new ArrayList<>();

        presenter=new BranchAddressPresenter(this);

//        doct_type类型,0=>未分配,1=>门诊,2=>住院,3=>体验,其他=>全部
        presenter.queryDoctor("","","",7);//查询所有的医生
        registerDoctorDetailAdapter=new RegisterDoctorDetailAdapter(this,doctorList);
        findDoctorList.setAdapter(registerDoctorDetailAdapter);
    }

    private void initViewData() {

//        for (int i=0;i<6;i++){
//            RegisterDoctorDetail registerDoctorDetail = new RegisterDoctorDetail();
//
//            doctorList.add(registerDoctorDetail);
//        }
        branchArray.add("鞍山金普医院");
        branchArray.add("营口金普分院");
        branchArray.add("沈阳金普分院");

        for (int j=0;j<6;j++){
            departmentArray.add("儿科");
        }
        typeArray.add("全部");
        typeArray.add("门诊");
        typeArray.add("住院");
        typeArray.add("体检");

        branchAdapter=new FindDoctorArrayAdapter(this,branchArray);
        departmentAdapter=new FindDoctorArrayAdapter(this,departmentArray);
        typeAdapter=new FindDoctorArrayAdapter(this,typeArray);

        branchList.setAdapter(branchAdapter);
        departmentList.setAdapter(departmentAdapter);
        typeList.setAdapter(typeAdapter);
        branchList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                cancleTableState();
                branchSelectTv.setTextColor(getResources().getColor(R.color.title_blue));
                branchSelect.setTextColor(getResources().getColor(R.color.title_blue));
//                branchSelectTv.setBackgroundDrawable(getResources().getDrawable(R.drawable.login_corner_blueedge));
                branchSelectTv.setText(branchArray.get(position).substring(0,4));
                branchSelectTv.setCompoundDrawablesWithIntrinsicBounds(null,null,getResources().getDrawable(R.drawable.jinpumingyi_jiantou),null);
                branchLL.setVisibility(View.GONE);

            }
        });
        typeList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                cancleTableState();
                typeSelectTv.setTextColor(getResources().getColor(R.color.title_blue));
                typeSelect.setTextColor(getResources().getColor(R.color.title_blue));
                typeSelectTv.setCompoundDrawablesWithIntrinsicBounds(null,null,getResources().getDrawable(R.drawable.jinpumingyi_jiantou),null);
//                typeSelectTv.setBackgroundDrawable(getResources().getDrawable(R.drawable.login_corner_blueedge));
                typeSelectTv.setText(typeArray.get(position));
                typeLL.setVisibility(View.GONE);


            }
        });
        departmentList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                cancleTableState();
                departmentSelectTv.setTextColor(getResources().getColor(R.color.title_blue));
                departmentSelect.setTextColor(getResources().getColor(R.color.title_blue));
                departmentSelectTv.setCompoundDrawablesWithIntrinsicBounds(null,null,getResources().getDrawable(R.drawable.jinpumingyi_jiantou),null);
//                departmentSelectTv.setBackgroundDrawable(getResources().getDrawable(R.drawable.login_corner_blueedge));
                departmentSelectTv.setText(departmentArray.get(position));
                departmentLL.setVisibility(View.GONE);


            }
        });

        findDoctorList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(FindDoctorActivity.this, DoctorDetailActivity.class);
                intent.putExtra("doctorId",doctorList.get(position).getUid()+"");
                intent.putExtra("doctorName",doctorList.get(position).getNickname());
                startActivity(intent);
            }
        });


    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()){
            case R.id.rlLeft:
            case R.id.leftImg:
                this.finish();
             break;
            case R.id.branchSelectLL:
            case R.id.branchSelect:
            case R.id.branchSelectTv:
                cancleTableState();
                tranSlate(0,300);
                branchLL.setVisibility(View.VISIBLE);
                departmentLL.setVisibility(View.GONE);
                typeLL.setVisibility(View.GONE);
                branchSelect.setTextColor(getResources().getColor(R.color.title_blue));
                branchSelectTv.setTextColor(getResources().getColor(R.color.title_blue));
                branchSelectTv.setCompoundDrawablesWithIntrinsicBounds(null,null,getResources().getDrawable(R.drawable.guahaodingwei_01),null);
//                branchSelectTv.setBackgroundDrawable(getResources().getDrawable(R.drawable.login_corner_blueedge));
                currIndex=0;

                break;
            case R.id.typeSelectLL:
            case R.id.typeSelect:
            case R.id.typeSelectTv:
                cancleTableState();
                tranSlate(1,300);
                typeLL.setVisibility(View.VISIBLE);

                branchLL.setVisibility(View.GONE);
                departmentLL.setVisibility(View.GONE);
                typeSelectTv.setCompoundDrawablesWithIntrinsicBounds(null,null,getResources().getDrawable(R.drawable.guahaodingwei_01),null);
                typeSelect.setTextColor(getResources().getColor(R.color.title_blue));
                typeSelectTv.setTextColor(getResources().getColor(R.color.title_blue));
//                typeSelectTv.setBackgroundDrawable(getResources().getDrawable(R.drawable.login_corner_blueedge));
                currIndex=1;
                break;
            case R.id.departmentSelectLL:
            case R.id.departmentSelect:
            case R.id.departmentSelectTv:
                cancleTableState();
                tranSlate(2,300);

                departmentLL.setVisibility(View.VISIBLE);
                branchLL.setVisibility(View.GONE);
                typeLL.setVisibility(View.GONE);

                departmentSelectTv.setCompoundDrawablesWithIntrinsicBounds(null,null,getResources().getDrawable(R.drawable.guahaodingwei_01),null);
                departmentSelectTv.setTextColor(getResources().getColor(R.color.title_blue));
//                departmentSelectTv.setBackgroundDrawable(getResources().getDrawable(R.drawable.login_corner_blueedge));

                //                departmentSelectTv.setCompoundDrawables();
                departmentSelect.setTextColor(getResources().getColor(R.color.title_blue));
                currIndex=2;
                break;
            case R.id.branchLL:
                if (branchLL.getVisibility()==View.VISIBLE){
                    branchLL.setVisibility(View.GONE);
                }

            case R.id.departmentLL:
                if (departmentLL.getVisibility()==View.VISIBLE){
                    departmentLL.setVisibility(View.GONE);
                }
                break;
            case R.id.typeLL:
                if (typeLL.getVisibility()==View.VISIBLE){
                typeLL.setVisibility(View.GONE);
            }
                break;


        }
    }

    /**
     * 平移动画
     *
     * @param currentItem
     * @param durationMillis
     */
    public void tranSlate(int currentItem, long durationMillis) {
        float one = moveDistance;
        Log.v("offest", "" + moveDistance);
        Animation animation = new TranslateAnimation(currIndex * one,
                currentItem * one, 0, 0);// 平移动画
        Log.v("currentoffest", "" + currIndex * one);
        Log.v("currentItem", "" + currentItem * one);
        animation.setFillAfter(true);// 动画终止时停留在最后一帧，不然会回到没有执行前的状态
        animation.setDuration(durationMillis);// 动画持续时间0.2秒
        movableLine.startAnimation(animation);// 是用ImageView来显示动画的
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        moveDistance = branchSelectLL.getMeasuredWidth();
        RelativeLayout.LayoutParams params=new RelativeLayout.LayoutParams(moveDistance,4);
        RelativeLayout.LayoutParams params1=new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        params.addRule(RelativeLayout.ALIGN_BOTTOM,R.id.selectLL);
        movableLine.setLayoutParams(params);
        int height = findDoctorList.getTop();
        Log.d("=====d=",height+"");
//        branchLL,departmentLL;
        params1.setMargins(0,height,0,0);
        branchLL.setLayoutParams(params1);
        departmentLL.setLayoutParams(params1);
        typeLL.setLayoutParams(params1);
        num=0;
        Log.d("==========",moveDistance+"");
    }

    public void cancleTableState(){
        branchSelect.setTextColor(getResources().getColor(R.color.black));
        branchSelectTv.setTextColor(getResources().getColor(R.color.black));
//        branchSelectTv.setBackgroundDrawable(getResources().getDrawable(R.drawable.login_corner_grayedge));
        branchSelectTv.setCompoundDrawablesWithIntrinsicBounds(null,null,getResources().getDrawable(R.drawable.jinpumingyi_jiantou),null);

        typeSelect.setTextColor(getResources().getColor(R.color.black));
        typeSelectTv.setTextColor(getResources().getColor(R.color.black));
//        typeSelectTv.setBackgroundDrawable(getResources().getDrawable(R.drawable.login_corner_grayedge));
        typeSelectTv.setCompoundDrawablesWithIntrinsicBounds(null,null,getResources().getDrawable(R.drawable.jinpumingyi_jiantou),null);

        departmentSelect.setTextColor(getResources().getColor(R.color.black));
        departmentSelectTv.setTextColor(getResources().getColor(R.color.black));
//        departmentSelectTv.setBackgroundDrawable(getResources().getDrawable(R.drawable.login_corner_grayedge));
        departmentSelectTv.setCompoundDrawablesWithIntrinsicBounds(null,null,getResources().getDrawable(R.drawable.jinpumingyi_jiantou),null);
    }

//    //分院筛选和种类筛选和科室筛选的点击逻辑
//    public void isClickShowView(View view){
//      num++;
//        if (id==view.getId()||id==0){
//
//        }else {
//         num++;
//        }
//      if (num%2==0){
//         view.setVisibility(View.GONE);
//      }else {
//          view.setVisibility(View.VISIBLE);
//      }
//        id=view.getId();
//    }


    @Override
    public void update(Observable o, Object arg) {
        ResultEntity resultEntity = handlerError(arg);
        if (resultEntity!=null){

            if (resultEntity.getEventType()==BranchAddressPresenter.By_HospDoct_query_success){
//                MAlert.alert("查询医师成功");
                QueryDoctorListBean queryDoctorListBean= (QueryDoctorListBean) resultEntity.getData();
                doctorList.addAll(queryDoctorListBean.getList());
                registerDoctorDetailAdapter.addData(doctorList);
            }else if (resultEntity.getEventType()==BranchAddressPresenter.By_HospDoct_query_fail){

//                MAlert.alert("查询医师失败");
            }
        }
    }
}
